// pages/go_wu_che_fenlei/go_wu_che_fenlei.js
import {
  request
} from "../../utils/http.js"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    allChecked: false, // 全选状态
    totalPrice: "0.00", // 总价
    totalCount: 0, // 总数量
    goods: [{
        url: "http://122.51.84.231:9001/dianshang/鸡蛋.png",
        text1: "语农 散装土鸡蛋 360枚 40斤",
        text2: "40斤 正负25g",
        money: 28.88,
        num: 1,
        checked: false,
      }
    ],

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getnums();
    request({
      url: "/getInfo",
      header: {
        "authorization": getApp().globalData.authorization
      }
    }).then(data => {
      if (data.code == 200) {
        request({
          url:"/dianshang/cart/listbyuserId?userid="+getApp().globalData.userInfo.userId,
          header: {
            "authorization": getApp().globalData.authorization
          }
        }).then(data=>{
          console.log(data);
          data.rows.map(item=>{
            return {...item,checked:false}
          })
          this.setData({
            goods:data.rows,
            totalCount:0
          })
          
        })
        
      }
      if (data.code == 401) {
        wx.navigateTo({
          url: '/pages/deng_lu/deng_lu',
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  gobuy() {
    const goods=this.data.goods;
    for(let i=0;i<goods.length;i++){
      if(!goods[i].checked){
        goods.splice(i,1);
        i--;
      }
    }
    wx.setStorageSync("goods",goods);
    wx.navigateTo({
      url: '/pages/go_wu_che/go_wu_che',
    })
  },
  // 单个商品勾选状态变化
  onItemCheckChange(e) {
    const index = e.currentTarget.dataset.index;
    const goods = this.data.goods;
    goods[index].checked = !goods[index].checked;
    this.updateTotal();
    if(this.data.totalCount==this.data.goods.length){
      this.setData({
        allChecked:true
      })
    }else{
      this.setData({
        allChecked:false
      })
    }
    this.setData({
      goods
    });
  },

  // 全选按钮变化
  onAllCheckChange(e) {
    const allChecked = e.detail;
    const goods = this.data.goods.map(item => ({
      ...item,
      checked: allChecked
    }));
    this.setData({
      goods,
      allChecked
    });
    this.updateTotal();
  },
  // 步进器变化
  onStepperChange(e) {
    const index = e.currentTarget.dataset.index;
    const goods = this.data.goods;
    goods[index].nums = e.detail;
    request({
      url:"/dianshang/cart",
      method:"PUT",
      header: {
        "authorization": getApp().globalData.authorization
      },
      data:{
        id:goods[index].id,
        nums:goods[index].nums
      }
    })
    this.updateTotal();
    this.setData({
      goods
    });
  },

  // 更新总价和总数量
  updateTotal() {
    const goods = this.data.goods;
    let totalPrice = 0.0;
    let totalCount = 0;

    goods.forEach(item => {
      if (item.checked) {
        totalPrice += item.aGoods.money * item.nums;
        totalCount += 1;
      }
    });

    this.setData({
      totalPrice: totalPrice.toFixed(2), // 保留两位小数
      totalCount
    });
  },
  getnums(){
    request({
      url:"/getInfo",
      header: {
        "authorization": getApp().globalData.authorization
      }
    }).then(data=>{
      if(data.code==200){
        request({
          url:"/dianshang/cart/list?userid="+getApp().globalData.userInfo.userId+"&pageSize=99",
          header: {
            "authorization": getApp().globalData.authorization
          }
        }).then(data1=>{
          console.log(data1);
          getApp().globalData.nums=data1.total;
          if(getApp().globalData.nums==0){
            wx.removeTabBarBadge({
              index:2
            })
          }else{
            wx.setTabBarBadge({
              index:2,
              text:getApp().globalData.nums.toString()
            })
          }
        })
      }else{
        getApp().globalData.nums=0;
        wx.removeTabBarBadge({
          index:2
        })
      }
    })
    
  }

})